package com.et.machine.manager.vm.goods

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.viewbinding.ViewBinding
import com.et.machine.util.Adapter
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class AdapterUseDiff<D : Any, V : ViewBinding>(
    call: (binding: V, p: Int, t: D) -> Unit,
    binding: (inflater: LayoutInflater, parent: ViewGroup) -> V,
) : Adapter<D, V>(call, binding) {

    fun dispatchUpdate(result: DiffUtil.DiffResult, dest: MutableList<D>) {
        CoroutineScope(Dispatchers.Main).launch {
            result.dispatchUpdatesTo(this@AdapterUseDiff)
            data = dest
        }
    }
}